home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / QuickDraw GX / SetDefaultDTP / SetDefaultDTP.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-13  |  10.4 KB  |  544 lines  |  [TEXT/CWIE]

  1. /********************************************************************************
  2.     SetDefaultDTP.c
  3.     
  4.     This file handles the main event loop and dialog events for SetDefaultDTP.
  5.     
  6.     ©1995  Apple Computer, Inc.
  7.     All rights reserved.
  8.  
  9. ********************************************************************************/
  10.  
  11.  
  12. #include <GestaltEqu.h>
  13. #include <AppleEvents.h>
  14.  
  15. #include "FSSetPrinter.h"
  16.  
  17.  
  18. #define kBaseResID            128
  19. #define kErrorALRTid        128
  20. #define kAboutALRTid        129
  21. #define kDialogResID        130
  22.  
  23. #define kVisible            true
  24. #define kMoveToFront        (WindowPtr)-1L
  25. #define kSleep                60L
  26. #define kNilFilterProc        0L
  27. #define kGestaltMask        1L
  28.  
  29. #define kOn                    1
  30. #define kOff                0
  31.  
  32. #define mApple                kBaseResID
  33. #define iAbout                1
  34.  
  35. #define mFile                kBaseResID+1
  36. #define iQuit                1
  37.  
  38. #define mEdit                kBaseResID+2
  39. #define iCut                1
  40. #define iCopy                2
  41. #define iPaste                3
  42. #define iClear                4
  43.  
  44. #define iGetText            1
  45. #define iText                3
  46.  
  47. #define kNoIdleProc            nil
  48. #define kNoFilterProc        nil
  49. #define kMaxTextSize        255
  50.  
  51.  
  52. /*************/
  53. /*  Globals  */
  54. /*************/
  55.  
  56. Boolean        gDone;
  57. DialogPtr    gDialogPtr;
  58.  
  59.  
  60. /***************/
  61. /*  Functions  */
  62. /***************/
  63.  
  64. void        ToolboxInit( void );
  65. void        MenuBarInit( void );
  66. void        AEInit( void );
  67. void        SetUpDLOG( void );
  68. void        EventLoop( void );
  69. void        DoEvent( EventRecord *eventPtr );
  70. void        DoDialogEvent( EventRecord *eventPtr );
  71. void        HandleMouseDown( EventRecord *eventPtr );
  72. void        HandleEditChoice( short item );
  73. void        HandleMenuChoice( long menuChoice );
  74. void        HandleAppleChoice( short item );
  75. void        HandleFileChoice( short item );
  76. void        DoError( Str255 errorString );
  77.  
  78. void InstallDefaultOutline(DialogPtr dlog, short button);
  79. void FlashDlogItem( DialogPtr dlog, short itemNum );
  80.     
  81.     
  82.     
  83.   typedef struct
  84. {
  85.     long            placeholder;
  86.     Rect            location;
  87.     unsigned char    itemType;
  88.     unsigned char    extraLength;
  89. } DITLitem;
  90.  
  91.  
  92.  
  93. /******************************** main *********/
  94.  
  95. void    main( void )
  96. {
  97.     ToolboxInit();
  98.     MenuBarInit();
  99.     AEInit();
  100.     
  101.     SetUpDLOG();
  102.     
  103.     EventLoop();
  104. }
  105.  
  106.  
  107. /****************** ToolboxInit *********************/
  108.  
  109. void    ToolboxInit( void )
  110. {
  111.     InitGraf( &qd.thePort );
  112.     InitFonts();
  113.     InitWindows();
  114.     InitMenus();
  115.     TEInit();
  116.     InitDialogs( 0L );
  117.     InitCursor();
  118. }
  119.  
  120.  
  121. /****************** MenuBarInit ***********************/
  122.  
  123. void    MenuBarInit( void )
  124. {
  125.     Handle            menuBar;
  126.     MenuHandle        menu;
  127.     
  128.     menuBar = GetNewMBar( kBaseResID );
  129.     
  130.     if ( menuBar == NULL )
  131.         DoError( "\pCouldn't load the MBAR resource..." );
  132.     
  133.     SetMenuBar( menuBar );
  134.  
  135.     menu = GetMHandle( mApple );
  136.     AddResMenu( menu, 'DRVR' );
  137.     
  138.     DrawMenuBar();
  139. }
  140.  
  141.  
  142. /******************************** AEInit *********/
  143.  
  144. void    AEInit( void )
  145. {
  146.     OSErr    err;
  147.     long    feature;
  148.     
  149.     err = Gestalt( gestaltAppleEventsAttr, &feature );
  150.     
  151.     if ( err != noErr )
  152.         DoError( "\pError returned by Gestalt!" );
  153.         
  154.     if ( !( feature & ( kGestaltMask << gestaltAppleEventsPresent ) ) )
  155.         DoError( "\pThis configuration does not support Apple events..." );
  156. }
  157.  
  158.  
  159. /****************** SetUpDLOG ***********************/
  160.  
  161. void    SetUpDLOG( void )
  162. {
  163.     gDialogPtr = GetNewDialog( kDialogResID, NULL, kMoveToFront );
  164.  
  165.     if ( gDialogPtr == NULL )
  166.         DoError( "\pCouldn't load the DLOG resource..." );
  167.         
  168.     InstallDefaultOutline(gDialogPtr,iGetText);
  169.     
  170.     ShowWindow( gDialogPtr );
  171.     SetPort( gDialogPtr );
  172.     
  173.     SelIText( gDialogPtr, iText, 0, 32767 );
  174.     TEFromScrap();
  175. }
  176.  
  177.  
  178. /******************************** EventLoop *********/
  179.  
  180. void    EventLoop( void )
  181. {        
  182.     EventRecord        event;
  183.     
  184.     gDone = false;
  185.     while ( gDone == false )
  186.     {
  187.         if ( WaitNextEvent( everyEvent, &event, kSleep, NULL ) )
  188.             DoEvent( &event );
  189.             
  190.         TEIdle( ((DialogPeek)gDialogPtr)->textH );
  191.     }
  192. }
  193.  
  194.  
  195. /************************************* DoEvent     */
  196.  
  197. void    DoEvent( EventRecord *eventPtr )
  198. {
  199.     if ( IsDialogEvent( eventPtr ) )
  200.     {
  201.         DoDialogEvent( eventPtr );
  202.     }
  203.     else if ( eventPtr->what == mouseDown )
  204.     {
  205.         HandleMouseDown( eventPtr );
  206.     }
  207. }
  208.  
  209.  
  210. /************************************* DoDialogEvent     */
  211.  
  212. void    DoDialogEvent( EventRecord *eventPtr )
  213. {
  214.     short        itemHit;
  215.     short        itemType;
  216.     Handle        itemHandle;
  217.     Rect        itemRect;
  218.     char        theChar;
  219.     DialogPtr    dialog;
  220.     Str255        theText;
  221.     long        menuAndItem;
  222.     Boolean        didEvent = false;
  223.  
  224.     switch ( eventPtr->what )
  225.     {
  226.         case keyDown:
  227.         case autoKey:
  228.             theChar = eventPtr->message & charCodeMask;
  229.             
  230.             if ( (eventPtr->modifiers & cmdKey) != 0 ) 
  231.             {
  232.                 menuAndItem = MenuKey( theChar );
  233.                 
  234.                 if ( HiWord( menuAndItem ) != 0 )
  235.                 {
  236.                     HandleMenuChoice( menuAndItem );
  237.                     return;
  238.                 }
  239.             }
  240.             else if (theChar == 0x0D || theChar == 0x03)    // return or enter
  241.             {
  242.                 FlashDlogItem(gDialogPtr,iGetText);
  243.                 GetDItem( gDialogPtr, iText, &itemType, &itemHandle, &itemRect );
  244.                 GetIText( itemHandle, theText );
  245.                 SendTestAE(theText);
  246.                 
  247.                 SelIText( gDialogPtr, iText, 0, 32767 );
  248.                 didEvent = true;
  249.             }
  250.             break;
  251.         case osEvt:
  252.             if (eventPtr->message & 0x01000000)
  253.                 if (eventPtr->message & resumeFlag)    // resume event
  254.                 {
  255.                     TEFromScrap();
  256.                 }
  257.                 else    // suspend event
  258.                 {
  259.                     ZeroScrap();
  260.                     TEToScrap();
  261.                 }
  262.             break;
  263.     }
  264.     
  265.     if (!didEvent)
  266.         if ( DialogSelect( eventPtr, &dialog, &itemHit ) )
  267.         {
  268.             switch ( itemHit )
  269.             {
  270.                 case iGetText:
  271.                     GetDItem( dialog, iText, &itemType, &itemHandle, &itemRect );
  272.                     GetIText( itemHandle, theText );
  273.                     SendTestAE(theText);
  274.                     
  275.                     SelIText( dialog, iText, 0, 32767 );
  276.                     break;
  277.             }
  278.         }
  279. }
  280.  
  281.  
  282. /************************************* HandleMouseDown */
  283.  
  284. void    HandleMouseDown( EventRecord *eventPtr )
  285. {
  286.     WindowPtr        window;
  287.     short            thePart;
  288.     long            menuChoice;
  289.     
  290.     thePart = FindWindow( eventPtr->where, &window );
  291.     
  292.     switch ( thePart )
  293.     {
  294.         case inMenuBar:
  295.             menuChoice = MenuSelect( eventPtr->where );
  296.             HandleMenuChoice( menuChoice );
  297.             break;
  298.         case inSysWindow : 
  299.             SystemClick( eventPtr, window );
  300.             break;
  301.         case inContent:
  302.             SelectWindow( window );
  303.             break;
  304.         case inDrag : 
  305.             DragWindow( window, eventPtr->where, &qd.screenBits.bounds );
  306.             break;
  307.     }
  308. }
  309.  
  310.  
  311. /****************** HandleMenuChoice ***********************/
  312.  
  313. void    HandleMenuChoice( long menuChoice )
  314. {
  315.     short    menu;
  316.     short    item;
  317.     
  318.     if ( menuChoice != 0 )
  319.     {
  320.         menu = HiWord( menuChoice );
  321.         item = LoWord( menuChoice );
  322.         
  323.         switch ( menu )
  324.         {
  325.             case mApple:
  326.                 HandleAppleChoice( item );
  327.                 break;
  328.             case mFile:
  329.                 HandleFileChoice( item );
  330.                 break;
  331.             case mEdit:
  332.                 HandleEditChoice( item );
  333.                 break;
  334.         }
  335.         HiliteMenu( 0 );
  336.     }
  337. }
  338.  
  339.  
  340. /****************** HandleAppleChoice ***********************/
  341.  
  342. void    HandleAppleChoice( short item )
  343. {
  344.     MenuHandle    appleMenu;
  345.     Str255        accName;
  346.     short        accNumber;
  347.     
  348.     switch ( item )
  349.     {
  350.         case iAbout:
  351.             NoteAlert( kAboutALRTid, NULL );
  352.             break;
  353.         default:
  354.             appleMenu = GetMHandle( mApple );
  355.             GetItem( appleMenu, item, accName );
  356.             accNumber = OpenDeskAcc( accName );
  357.             break;
  358.     }
  359. }
  360.  
  361.  
  362. /****************** HandleFileChoice ***********************/
  363.  
  364. void    HandleFileChoice( short item )
  365. {
  366.     switch ( item )
  367.     {
  368.         case iQuit:
  369.             gDone = true;
  370.             break;
  371.     }
  372. }
  373.  
  374.  
  375. /****************** HandleEditChoice ***********************/
  376.  
  377. void    HandleEditChoice( short item )
  378. {
  379.     switch ( item )
  380.     {
  381.         case iCut:
  382.             DialogCut(gDialogPtr);
  383.             break;
  384.         case iCopy:
  385.             DialogCopy(gDialogPtr);
  386.             break;
  387.         case iPaste:
  388.             DialogPaste(gDialogPtr);
  389.             break;
  390.         case iClear:
  391.             DialogDelete(gDialogPtr);
  392.             break;
  393.     }
  394. }
  395.  
  396.  
  397. /***************** DoError ********************/
  398.  
  399. void    DoError( Str255 errorString )
  400. {
  401.     ParamText( errorString, "\p", "\p", "\p" );
  402.     
  403.     StopAlert( kErrorALRTid, kNilFilterProc );
  404.     
  405.     ExitToShell();
  406. }
  407.  
  408.  
  409. Boolean DialogItemEnabled(DialogPtr dlog, short item)
  410. {
  411.     ControlHandle        buttonHandle = nil;
  412.     Rect                buttonBox;
  413.     short                type;
  414.  
  415.     GetDItem(dlog, item, &type, (Handle*)&buttonHandle, &buttonBox);
  416.     return ((type & itemDisable) == false);
  417. } // DialogItemEnabled 
  418.  
  419.  
  420. pascal void DrawDefaultProc(DialogPtr dlog, short item)
  421. {
  422.     short                defaultButton;
  423.     
  424.     //
  425.     // Don't call GetDItem if the default button # has a strange value
  426.     //
  427.     defaultButton = ((DialogPeek)dlog)->aDefItem;
  428.     if( defaultButton > 0 )
  429.     {
  430.         short                type;
  431.         Handle                userHandle;
  432.         Rect                outlineBox;
  433.  
  434.         //
  435.         // Only draw the bold outline around the default button
  436.         // if it really is a button
  437.         //
  438.         GetDItem(dlog, defaultButton, &type, &userHandle, &outlineBox);
  439.         if( (type & (ctrlItem + btnCtrl)) == ctrlItem + btnCtrl )
  440.         {
  441.             PenState            saveState;
  442.             short                buttonOval;
  443.  
  444.             GetPenState( &saveState );
  445.             InsetRect(&outlineBox,-4,-4);
  446.             
  447.             //
  448.             // We want to draw the thick line with a normal
  449.             // pen pattern that is 3 pixels wide
  450.             //
  451.             PenNormal();
  452.             PenSize(3,3);
  453.             PenMode(srcCopy);
  454.             
  455.             //
  456.             // If the button we are outlining is disabled,
  457.             // draw the outline with a gray pattern.
  458.             //
  459.             if( !DialogItemEnabled(dlog, defaultButton ) )
  460.             {
  461.                 PenPat(&qd.gray);
  462.             }
  463.             
  464.             //
  465.             // Calculate the curvature to use and draw the thick line
  466.             //
  467.             buttonOval = 2 + (outlineBox.bottom - outlineBox.top) / 2;
  468.             FrameRoundRect(&outlineBox,buttonOval,buttonOval);
  469.             
  470.             SetPenState( &saveState );
  471.         }
  472.     }
  473. } // DrawDefaultProc 
  474.  
  475.  
  476. short AddNewUserItem( DialogPtr dlog )
  477. {
  478.     DialogPeek        theDialog        = (DialogPeek)dlog;
  479.     short**            itemHandle        = (short**)theDialog->items;
  480.     short            nItems            = **itemHandle + 1;
  481.     short            newItem            = 0;
  482.     DITLitem*        ditlPtr;
  483.     Size            itemHandleSize;
  484.     
  485.     itemHandleSize = GetHandleSize( (Handle)itemHandle );
  486.     SetHandleSize( (Handle)itemHandle, itemHandleSize + sizeof(DITLitem) );
  487.     if( MemError() == noErr )
  488.     {
  489.         ditlPtr = (DITLitem*) ( (*itemHandle) + (itemHandleSize / sizeof(short)) );
  490.         
  491.         ditlPtr->placeholder = 0;
  492.         ditlPtr->itemType = userItem;
  493.         ditlPtr->extraLength = 0;
  494.         
  495.         newItem = nItems + 1;
  496.         **itemHandle = newItem - 1;
  497.     }
  498.     
  499.     return newItem;
  500. } // AddNewUserItem 
  501.  
  502.  
  503.  
  504. void InstallDefaultOutline(DialogPtr dlog, short button)
  505. {
  506.     short            userItem;
  507.     short            type;
  508.     Handle            item;
  509.     Rect            box;
  510.     Rect            userBox;
  511.     
  512.     ((DialogPeek)dlog)->aDefItem = button;
  513.     userItem = AddNewUserItem( dlog );
  514.     if( userItem > 0 )
  515.     {
  516.         GetDItem(dlog, button, &type, &item, &box);
  517.         InsetRect(&box,-4,-4);
  518.         GetDItem(dlog, userItem, &type, &item, &userBox);
  519. #if USESROUTINEDESCRIPTORS
  520.         SetDItem(dlog,userItem,type,(Handle)&gDrawDefaultProcRD,&userBox);
  521. #else
  522.         SetDItem(dlog,userItem,type,(Handle)DrawDefaultProc,&userBox);
  523. #endif
  524.     }
  525. }
  526.  
  527.  
  528.  
  529. void FlashDlogItem( DialogPtr dlog, short itemNum )
  530. {
  531.     Handle        itemHandle;
  532.     short        itemType;
  533.     Rect        iRect;
  534.     long        tock;
  535.     
  536.     GetDItem(dlog,itemNum,&itemType,&itemHandle,&iRect);
  537.     HiliteControl((ControlHandle)itemHandle,1);
  538.     Draw1Control((ControlHandle)itemHandle);
  539.     tock = TickCount() + 10;
  540.     do {} while( tock > TickCount() );
  541.     HiliteControl((ControlHandle)itemHandle,0);
  542.     Draw1Control((ControlHandle)itemHandle);
  543. } // FlashDlogItem 
  544.